using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace KFGame
{
    /// <summary>
    /// 吐券类设备实例
    /// </summary>
    public class KFTicketDeviceIOCOM : MonoBehaviour
    {

        int number = 0;
        KF kF;
        private KFSeralPort port;


        /// <summary>
        /// 发出通知标题
        /// </summary>
        public string noticeTitle = "通知";
        /// <summary>
        /// 发出通知内容
        /// </summary>
        public string noticketMesage = "设备没有票券，请与管理员联系";
        /// <summary>
        /// 发出通知的名字
        /// </summary>
        public string noticeName = "ticket";
        /// <summary>
        /// 使用设备名称
        /// </summary>
        public string device = "ticket";


        /// <summary>
        /// 设备接口名称
        /// </summary>
        public string device_Com_name = "COM1";

        /// <summary>
        /// 接口发送事信息接口名
        /// </summary>
        public string device_io_send_eventname = "send_Ticket";
        /// <summary>
        /// 接口接收信息接口名
        /// </summary>
        public string device_io_callback_eventname = "KF_Ticket_back";
        /// <summary>
        /// 接口接收信息接口名
        /// </summary>
        public string device_io_stop_eventname = "stop_Ticket";


        /// <summary>
        /// 超时时间
        /// </summary>
        public float OverTime = 5f;

        public delegate void SendTicket(string name, int number);
        public SendTicket onSendTicket;


        public void addListener(SendTicket onTicket){
            onSendTicket +=onTicket;
        }
        public void removeListener(SendTicket onTicket){
             onSendTicket -=onTicket;
        }

        /// <summary>
        ///  使用共享方试，以使不同场景中使用
        /// </summary>

        private static KFTicketDeviceIOCOM ticket_Device_IO_COM;
        public static KFTicketDeviceIOCOM Instantiation()
        {
            if (ticket_Device_IO_COM == null)
                ticket_Device_IO_COM = KF.kf.gameObject.AddComponent<KFTicketDeviceIOCOM>();

            return ticket_Device_IO_COM;
        }


        /// <summary>
        /// 发送
        /// </summary>
        /// <param name="a"></param>
        public void send(int a = 0)
        {
            number += a;
            sendDatas(a);
        }

        /// <summary>
        /// 减少
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        bool remove(int a = 1)
        {
            number -= a;
            if (number < 0)
            {
                number = 0;
                return false;
            }
            return true;
        }


        /// <summary>
        /// 发送数据到设备
        /// </summary>
        /// <param name="a"></param>
        void sendDatas(int a)
        {
            //有提示信息先停止
            closeNotice(noticeName);

            //计时器 先停止再启动
            if (otime != null)
                StopCoroutine(otime);

            otime = StartCoroutine(tickTime());


            OutputData data = new OutputData(KFSeralPort.getEventMode(device_io_send_eventname));
            if (data.isEnd)
                data.EndData = new OutputData(KFSeralPort.getEventMode(data.EndDataName));

            data.addSendNumber(a);

            //配置参数
            if (port != null)
                port.addSend(data);
        }



        /// <summary>
        /// 设备返回数据
        /// </summary>
        /// <param name="fiter"></param>
        void listener(DataFiter fiter)
        {
            // Debug.Log(">>>>>>>>>>>>>>>>> 1 get fiter " + fiter.name + ":" + fiter.ischeck);
            if (device_io_callback_eventname == fiter.name)
            {

                if (fiter.ischeck)
                {
                    // Debug.Log(">>>>>>>>>>>>>>>>> 2 get fiter " + "back" + ":" + ":" + 1);
                    closeNotice(noticeName);
                    if (otime != null)
                        StopCoroutine(otime);


                    //是否正常计数据，正常计数进入下一轮
                    if (remove())
                    {
                        // Debug.Log(">>>>>>>>>>>>>>>>>3 to  " + "back " + number + ":" + 1);
                        if (number > 0)
                            otime = StartCoroutine(tickTime());

                        if (onSendTicket != null)
                            onSendTicket(device, 1);
                    }
                }
            }


        }

        /// <summary>
        ///  发送通知
        /// </summary>
        /// <param name="name"></param>
        /// <param name="msg"></param>
        public void sendNotice(string name, string title, string msg)
        {
            if (kF.app != null)
                kF.app.showMessageBox(msg, title, 0, name);
        }

        /// <summary>
        /// 关闭通知
        /// </summary>
        /// <param name="name"></param>
        public void closeNotice(string name)
        {
            if (kF.app != null)
                kF.app.closeMessageBox(name);
        }

        /// <summary>
        /// 初始化 
        /// </summary>
        void Start()
        {
            kF = KF.Instantiation();
        }

        /// <summary>
        /// 配置完参数再运行初始化
        /// </summary>
        public void init()
        {

            port = KFSeralPort.GetSeralPort(device_Com_name);
            port.addListener(listener);

            DataFiter ticketFiter = new DataFiter(KFSeralPort.getEventMode(device_io_callback_eventname));
            port.addFiter(ticketFiter);
        }


        /// <summary>
        /// 页面关闭前需关闭一些操作
        /// </summary>
        public void viewClose()
        {
            if (port != null)
                port.removeListener(listener);
        }


        Coroutine otime;
        /// <summary>
        /// 计时器
        /// </summary>
        /// <returns></returns>
        IEnumerator tickTime()
        {
            yield return new WaitForSecondsRealtime(OverTime);
            sendNotice(noticeName, noticeTitle, noticketMesage);
        }
    }
}
